package com.bw.gmall.realtime.app.dws;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.app.func.DimAsyncFunction;
import com.bw.gmall.realtime.bean.TradeUserSpuOrderBean;
import com.bw.gmall.realtime.utils.*;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichFilterFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple4;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class DwsTradeUserSpuOrderWindow {
    public static void main(String[] args) throws Exception {
        //todo 1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        
        
        //todo 2.读取kafka dwd层下单主题数据创建流
        String topic ="dwd_trade_order_detail";
        String groupId ="dwd_trade_user_spu_order_window_211126";
        DataStreamSource<String> kafkaDS = env.addSource(MyKafkaUtil.getFlinkKafkaConsumer(topic, groupId));

        //todo 3.将每行数据转换为JSON对象
        SingleOutputStreamOperator<JSONObject> jsonObjDS = kafkaDS.flatMap(new FlatMapFunction<String, JSONObject>() {
            @Override
            public void flatMap(String s, Collector<JSONObject> collector) throws Exception {
                try {
                    JSONObject jsonObject = JSON.parseObject(s);
                    collector.collect(jsonObject);
                } catch (Exception e) {
                    System.out.println("Value>>>>>>>>>" + s);
                }
            }
        });

        //todo 4.按照order_detail_id 分组(关联的数据非常多  我只要订单明细和订单就ok  这一块是去重操作)
        KeyedStream<JSONObject, String> keyedByDetailIdDS = jsonObjDS.keyBy(x -> x.getString("id"));

        //todo 5.针对 order_detail_id 进行去重(保留第一条数据即可)
        SingleOutputStreamOperator<JSONObject> filterDS = keyedByDetailIdDS.filter(new RichFilterFunction<JSONObject>() {
            private ValueState<String> valueState;

            @Override
            public void open(Configuration parameters) throws Exception {
                //创建状态的生命周期
                StateTtlConfig build = new StateTtlConfig.Builder(Time.seconds(5)).setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite).build();

                ValueStateDescriptor<String> valueStateDescriptor = new ValueStateDescriptor<>("valueState", String.class);
                valueStateDescriptor.enableTimeToLive(build);
                valueState = getRuntimeContext().getState(valueStateDescriptor);

            }

            @Override
            public boolean filter(JSONObject jsonObject) throws Exception {
                //获取状态数据
                String state = valueState.value();

                //判断状态是否为null
                if (state == null) {
                    valueState.update("1");
                    return true;
                } else {
                    return false;
                }
            }
        });

        //订单明细订单
        //todo 6. 将数据转换为JavaBean对象
        SingleOutputStreamOperator<TradeUserSpuOrderBean> tradeUserSpuDS = filterDS.map(x -> {
            HashSet<String> orderIds = new HashSet<>();
            orderIds.add(x.getString("order_id"));
            return TradeUserSpuOrderBean.builder()
                    .skuId(x.getString("sku_id"))
                    .userId(x.getString("user_id"))
                    .orderAmount(x.getDouble("split_total_amount"))
                    .orderIdSet(orderIds)
                    .ts(DateFormatUtil.toTs(x.getString("create_time"), true))
                    .build();

        });
        tradeUserSpuDS.print("tradeUserSpuDS>>>>>>>>>>>>>>>>");


        //TODO 7.关联sku_info 维表 补充userId,spu_id,tm_id,category3_id
        //map 必须一条一条处理
        SingleOutputStreamOperator<TradeUserSpuOrderBean> map = tradeUserSpuDS.map(new RichMapFunction<TradeUserSpuOrderBean, TradeUserSpuOrderBean>() {
            DruidDataSource dataSource;

            @Override
            public void open(Configuration parameters) throws Exception {
                //创建Phoenix连接池
                dataSource = DruidDSUtil.createDataSource();
            }

            @Override
            public TradeUserSpuOrderBean map(TradeUserSpuOrderBean v) throws Exception {
                //查询维表，将查到的信息补充至JavaBean中
                DruidPooledConnection connection = dataSource.getConnection();
                //这个工具类说白了就是把你查询结果  封装到对象里面中(key -value ->->对应)
                List<JSONObject> jsonObjects = JdbcUtil.queryList(connection, "select * from GMALL_REALTIME.DIM_SKU_INFO where id= '" + v.getSkuId() + "'"
                        , JSONObject.class, true);
                //tm_id-tmId
                //tmId  category3Id
                v.setSpuId(jsonObjects.get(0).getString("spuId"));
                v.setTrademarkId(jsonObjects.get(0).getString("tmId"));
                v.setCategory3Id(jsonObjects.get(0).getString("category3Id"));
                return v;


            }
        });
        map.print();

        //AsyncDataStream 异步处理

        /*
        hbase 维度表进行关联的时候需要什么
        表名 key 合并
         */

        SingleOutputStreamOperator<TradeUserSpuOrderBean> tradeUserSpuWithSkuDS = AsyncDataStream.unorderedWait(tradeUserSpuDS,
                //异步io
                new DimAsyncFunction<TradeUserSpuOrderBean>("DIM_SKU_INFO") {

                    @Override
                    public String getKey(TradeUserSpuOrderBean input) {
                        return input.getSkuId();
                    }

                    @Override
                    public void join(TradeUserSpuOrderBean input, JSONObject dimInfo) {
                        input.setSpuId(dimInfo.getString("SPU_ID"));
                        input.setTrademarkId(dimInfo.getString("TM_ID"));
                        input.setCategory3Id(dimInfo.getString("CATEGORY3_ID"));

                    }
                }, 100, TimeUnit.SECONDS);
        
                //TradeUserSpuOrderBean  skuId   tmId  c3 orderId
        tradeUserSpuWithSkuDS.print("tradeUserSpuWithSkuDS>>>>>>>>>>>>>>>>>>>");
        
        //todo 8.提取事件时间生成Watermark
        SingleOutputStreamOperator<TradeUserSpuOrderBean> tradeUserSpuWithWmDS = tradeUserSpuWithSkuDS.assignTimestampsAndWatermarks(WatermarkStrategy.<TradeUserSpuOrderBean>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                .withTimestampAssigner(new SerializableTimestampAssigner<TradeUserSpuOrderBean>() {
                    @Override
                    public long extractTimestamp(TradeUserSpuOrderBean ele, long l) {
                        return ele.getTs();
                    }
                }));

        //TODO 9.分组、开窗、聚合
        KeyedStream<TradeUserSpuOrderBean, Tuple4<String, String, String, String>> keyedStream = tradeUserSpuWithWmDS.keyBy(new KeySelector<TradeUserSpuOrderBean, Tuple4<String, String, String, String>>() {
            @Override
            public Tuple4<String, String, String, String> getKey(TradeUserSpuOrderBean v) throws Exception {
                return new Tuple4<>(
                        v.getUserId(),
                        v.getSpuId(),
                        v.getTrademarkId(),
                        v.getCategory3Id());
            }
        });

        SingleOutputStreamOperator<TradeUserSpuOrderBean> reduceDS = keyedStream.window(TumblingEventTimeWindows.of(org.apache.flink.streaming.api.windowing.time.Time.seconds(10)))
                .reduce(new ReduceFunction<TradeUserSpuOrderBean>() {
                    @Override
                    public TradeUserSpuOrderBean reduce(TradeUserSpuOrderBean v1, TradeUserSpuOrderBean v2) throws Exception {
                        v1.getOrderIdSet().addAll(v2.getOrderIdSet());
                        v1.setOrderAmount(v1.getOrderAmount() + v2.getOrderAmount());
                        return v1;
                    }
                }, new WindowFunction<TradeUserSpuOrderBean, TradeUserSpuOrderBean, Tuple4<String, String, String, String>, TimeWindow>() {
                    @Override
                    public void apply(Tuple4<String, String, String, String> key, TimeWindow timeWindow, Iterable<TradeUserSpuOrderBean> input, Collector<TradeUserSpuOrderBean> collector) throws Exception {
                        TradeUserSpuOrderBean userSpuOrderBean = input.iterator().next();
                        userSpuOrderBean.setTs(System.currentTimeMillis());
                        userSpuOrderBean.setOrderCount((long) userSpuOrderBean.getOrderIdSet().size());
                        userSpuOrderBean.setStt(DateFormatUtil.toYmdHms(timeWindow.getStart()));
                        userSpuOrderBean.setEdt(DateFormatUtil.toYmdHms(timeWindow.getEnd()));
                        collector.collect(userSpuOrderBean);
                    }
                });

        //spuId c3
        //todo 10.关联spu,tm,category维表补充相应的信息
        //10.1关联spu表
        SingleOutputStreamOperator<TradeUserSpuOrderBean> reduceWithSpuDS = AsyncDataStream.unorderedWait(reduceDS, new DimAsyncFunction<TradeUserSpuOrderBean>("DIM_SPU_INFO") {

            @Override
            public String getKey(TradeUserSpuOrderBean input) {
                return input.getSpuId();
            }

            @Override
            public void join(TradeUserSpuOrderBean input, JSONObject dimInfo) {
                input.setSpuName(dimInfo.getString("SPU_NAME"));
            }
        }, 100, TimeUnit.SECONDS);


        //10.2关联Tm表
        SingleOutputStreamOperator<TradeUserSpuOrderBean> reduceWithTmDS = AsyncDataStream.unorderedWait(reduceWithSpuDS, new DimAsyncFunction<TradeUserSpuOrderBean>("DIM_BASE_TRADEMARK") {

            @Override
            public String getKey(TradeUserSpuOrderBean input) {
                return input.getTrademarkId();
            }

            @Override
            public void join(TradeUserSpuOrderBean input, JSONObject dimInfo) {
                input.setTrademarkName(dimInfo.getString("TM_NAME"));
            }
        }, 100, TimeUnit.SECONDS);
        //10.3 关联Category3
        SingleOutputStreamOperator<TradeUserSpuOrderBean> reduceWithCategory3DS = AsyncDataStream.unorderedWait(reduceWithTmDS, new DimAsyncFunction<TradeUserSpuOrderBean>("DIM_BASE_CATEGORY3") {

            @Override
            public String getKey(TradeUserSpuOrderBean input) {
                return input.getCategory3Id();
            }

            @Override
            public void join(TradeUserSpuOrderBean input, JSONObject dimInfo) {
                input.setCategory3Name(dimInfo.getString("NAME"));
                input.setCategory2Id(dimInfo.getString("CATEGORY2_ID"));
            }
        }, 100, TimeUnit.SECONDS);

        //10.4  关联Category2
        SingleOutputStreamOperator<TradeUserSpuOrderBean> reduceWithCategory2DS = AsyncDataStream.unorderedWait(reduceWithCategory3DS, new DimAsyncFunction<TradeUserSpuOrderBean>("DIM_BASE_CATEGORY2") {

            @Override
            public String getKey(TradeUserSpuOrderBean input) {
                return input.getCategory2Id();
            }

            @Override
            public void join(TradeUserSpuOrderBean input, JSONObject dimInfo) {
                input.setCategory2Name(dimInfo.getString("NAME"));
                input.setCategory1Id(dimInfo.getString("CATEGORY1_ID"));
            }
        }, 100, TimeUnit.SECONDS);

        //10.5  关联Category1

        SingleOutputStreamOperator<TradeUserSpuOrderBean> reduceWithCategory1DS = AsyncDataStream.unorderedWait(reduceWithCategory2DS, new DimAsyncFunction<TradeUserSpuOrderBean>("DIM_BASE_CATEGORY1") {

            @Override
            public String getKey(TradeUserSpuOrderBean input) {
                return input.getCategory1Id();
            }

            @Override
            public void join(TradeUserSpuOrderBean input, JSONObject dimInfo) {
                input.setCategory1Name(dimInfo.getString("NAME"));
            }
        }, 100, TimeUnit.SECONDS);

        //todo 11.将数据写出到ClickHouse
        reduceWithCategory1DS.print(">>>>>>>>>>>>>");
        reduceWithCategory1DS.addSink(MyClickHouseUtil.getSinkFunction("insert into dws_trade_user_spu_order_window values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"));

        //todo 12.启动
        env.execute("DwsTradeUserSpuOrderWindow");


    }
}
